﻿using System;
using System.Collections.Generic;
using System.Net.Http.Headers;
using System.Text;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;

namespace Kugar.UI.Web.AdminLTE.NetCore.Resource
{
    public static class GlobalConfigurationHelper
    {
        /// <summary>
        /// 启用AdminLTEUI组件
        /// </summary>
        /// <param name="services"></param>
        public static void UseAdminLTE(this IServiceCollection services)
        {
            var embeddedProvider = new EmbeddedFileProvider(typeof(GlobalConfigurationHelper).Assembly, "Kugar.UI.Web.AdminLTE.NetCore.Resource");

            services.Configure<RazorViewEngineOptions>(options =>
            {
                options.FileProviders.Add(embeddedProvider);
            });

        }

        /// <summary>
        /// 是否使用内嵌的静态资源
        /// </summary>
        /// <param name="app"></param>
        public static void UserAdminLTEStaticFile(this IApplicationBuilder app)
        {

            var embeddedProvider = new EmbeddedFileProvider(typeof(GlobalConfigurationHelper).Assembly, "Kugar.UI.Web.AdminLTE.NetCore.Resource");

            app.UseStaticFiles(new StaticFileOptions
                               {
                                   FileProvider = embeddedProvider,
                                   RequestPath = new PathString("/adminltebase"),
                                   OnPrepareResponse = (context) => {
                                       var headers = context.Context.Response.GetTypedHeaders();
                                       headers.CacheControl = new CacheControlHeaderValue
                                                              {
                                                                  Public = true,
                                                                  MaxAge = TimeSpan.FromDays(365)
                                                              };
                                   }
                               });
        }

    }
}
